#ifndef _UTILS_E8454BED_EFD3_4113_B64F_A982D1A55E99_
#define _UTILS_E8454BED_EFD3_4113_B64F_A982D1A55E99_

class AuxLoadLibrary {
public:
  AuxLoadLibrary(HINSTANCE hinst = NULL) : hinst_(hinst) {
  }

  AuxLoadLibrary(const wchar_t* dllfile)  : hinst_(NULL) {
    Load(dllfile);
  }

  ~AuxLoadLibrary() {
    Free();
  }

  bool Load(const wchar_t* dllfile, DWORD flags = 0) {
    Free();
    hinst_ = ::LoadLibraryExW(dllfile, NULL, flags);
    return (hinst_ != NULL) ? TRUE : FALSE;
  }

  void Free() {
    if (IsLoaded()) {
      ::FreeLibrary(hinst_);
      hinst_ = NULL;
    }
  }

  HINSTANCE Detach() {
    HINSTANCE hinst = hinst_;
    hinst_ = NULL;
    return hinst;
  }

  bool IsLoaded() const  { 
    return (hinst_ != NULL) ? TRUE : FALSE; 
  }

  FARPROC GetProcAddress(const char* func) const  { 
    return ::GetProcAddress(hinst_, func);
  }

  operator HINSTANCE() const { return hinst_; }

private:
  HINSTANCE hinst_;
};

#endif//_UTILS_E8454BED_EFD3_4113_B64F_A982D1A55E99_
